home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / gcl-1.000 / gcl-1 / gcl-1.0 / c / malloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-07  |  22.3 KB  |  787 lines

  1. /* dynamic memory allocation for GNU.
  2.    Copyright (C) 1985, 1987 Free Software Foundation, Inc.
  3.  
  4.                NO WARRANTY
  5.  
  6.   BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  7. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  8. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  9. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  10. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  11. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  13. AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  14. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  15. CORRECTION.
  16.  
  17.  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  18. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  19. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  20. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  21. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  22. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  23. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  24. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  25. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  26. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  27.  
  28.         GENERAL PUBLIC LICENSE TO COPY
  29.  
  30.   1. You may copy and distribute verbatim copies of this source file
  31. as you receive it, in any medium, provided that you conspicuously and
  32. appropriately publish on each copy a valid copyright notice "Copyright
  33. (C) 1985 Free Software Foundation, Inc."; and include following the
  34. copyright notice a verbatim copy of the above disclaimer of warranty
  35. and of this License.  You may charge a distribution fee for the
  36. physical act of transferring a copy.
  37.  
  38.   2. You may modify your copy or copies of this source file or
  39. any portion of it, and copy and distribute such modifications under
  40. the terms of Paragraph 1 above, provided that you also do the following:
  41.  
  42.     a) cause the modified files to carry prominent notices stating
  43.     that you changed the files and the date of any change; and
  44.  
  45.     b) cause the whole of any work that you distribute or publish,
  46.     that in whole or in part contains or is a derivative of this
  47.     program or any part thereof, to be licensed at no charge to all
  48.     third parties on terms identical to those contained in this
  49.     License Agreement (except that you may choose to grant more extensive
  50.     warranty protection to some or all third parties, at your option).
  51.  
  52.     c) You may charge a distribution fee for the physical act of
  53.     transferring a copy, and you may at your option offer warranty
  54.     protection in exchange for a fee.
  55.  
  56. Mere aggregation of another unrelated program with this program (or its
  57. derivative) on a volume of a storage or distribution medium does not bring
  58. the other program under the scope of these terms.
  59.  
  60.   3. You may copy and distribute this program (or a portion or derivative
  61. of it, under Paragraph 2) in object code or executable form under the terms
  62. of Paragraphs 1 and 2 above provided that you also do one of the following:
  63.  
  64.     a) accompany it with the complete corresponding machine-readable
  65.     source code, which must be distributed under the terms of
  66.     Paragraphs 1 and 2 above; or,
  67.  
  68.     b) accompany it with a written offer, valid for at least three
  69.     years, to give any third party free (except for a nominal
  70.     shipping charge) a complete machine-readable copy of the
  71.     corresponding source code, to be distributed under the terms of
  72.     Paragraphs 1 and 2 above; or,
  73.  
  74.     c) accompany it with the information you received as to where the
  75.     corresponding source code may be obtained.  (This alternative is
  76.     allowed only for noncommercial distribution and only if you
  77.     received the program in object code or executable form alone.)
  78.  
  79. For an executable file, complete source code means all the source code for
  80. all modules it contains; but, as a special exception, it need not include
  81. source code for modules which are standard libraries that accompany the
  82. operating system on which the executable file runs.
  83.  
  84.   4. You may not copy, sublicense, distribute or transfer this program
  85. except as expressly provided under this License Agreement.  Any attempt
  86. otherwise to copy, sublicense, distribute or transfer this program is void and
  87. your rights to use the program under this License agreement shall be
  88. automatically terminated.  However, parties who have received computer
  89. software programs from you with this License Agreement will not have
  90. their licenses terminated so long as such parties remain in full compliance.
  91.  
  92.   5. If you wish to incorporate parts of this program into other free
  93. programs whose distribution conditions are different, write to the Free
  94. Software Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
  95. worked out a simple rule that can be stated here, but we will often permit
  96. this.  We will be guided by the two goals of preserving the free status of
  97. all derivatives of our free software and of promoting the sharing and reuse of
  98. software.
  99.  
  100.  
  101. In other words, you are welcome to use, share and improve this program.
  102. You are forbidden to forbid anyone else to use, share and improve
  103. what you give them.   Help stamp out software-hoarding!  */
  104.  
  105.  
  106. /*
  107.  * @(#)nmalloc.c 1 (Caltech) 2/21/82
  108.  *
  109.  *    U of M Modified: 20 Jun 1983 ACT: strange hacks for Emacs
  110.  *
  111.  *    Nov 1983, Mike@BRL, Added support for 4.1C/4.2 BSD.
  112.  *
  113.  * This is a very fast storage allocator.  It allocates blocks of a small 
  114.  * number of different sizes, and keeps free lists of each size.  Blocks
  115.  * that don't exactly fit are passed up to the next larger size.  In this 
  116.  * implementation, the available sizes are (2^n)-4 (or -16) bytes long.
  117.  * This is designed for use in a program that uses vast quantities of
  118.  * memory, but bombs when it runs out.  To make it a little better, it
  119.  * warns the user when he starts to get near the end.
  120.  *
  121.  * June 84, ACT: modified rcheck code to check the range given to malloc,
  122.  * rather than the range determined by the 2-power used.
  123.  *
  124.  * Jan 85, RMS: calls malloc_warning to issue warning on nearly full.
  125.  * No longer Emacs-specific; can serve as all-purpose malloc for GNU.
  126.  * You should call malloc_init to reinitialize after loading dumped Emacs.
  127.  * Call malloc_stats to get info on memory stats if MSTATS turned on.
  128.  * realloc knows how to return same block given, just changing its size,
  129.  * if the power of 2 is correct.
  130.  */
  131. /*  Oct 89: wfs@cs.utexas.edu:  Created  V/ merge file for
  132.  * changes for GCL.
  133.  * Calls to sbrk replaced by alloc_page.   Remove some of the
  134.  * additions for emacs.
  135.  * NB:  According to the gnu license you may only distribute the
  136.  * verbatim copy of the gnumalloc.c.   Thus we only distribute
  137.  * an abbreviated diffs file from that verbatim copy.
  138. */
  139. /*
  140.  * nextf[i] is the pointer to the next free block of size 2^(i+3).  The
  141.  * smallest allocatable block is 8 bytes.  The overhead information will
  142.  * go in the first int of the block, and the returned pointer will point
  143.  * to the second.
  144.  *
  145. #ifdef MSTATS
  146.  * nmalloc[i] is the difference between the number of mallocs and frees
  147.  * for a given block size.
  148. #endif /* MSTATS */
  149.  
  150. #ifdef emacs
  151. #include "config.h"
  152. #endif /* emacs */
  153.  
  154. /* Determine which kind of system this is.  */
  155. #include <signal.h>
  156. #ifndef SIGTSTP
  157. #ifndef VMS
  158. #ifndef USG
  159. #define USG
  160. #endif
  161. #endif /* not VMS */
  162. #else /* SIGTSTP */
  163. #ifdef SIGIO
  164. #define BSD42
  165. #endif /* SIGIO */
  166. #endif /* SIGTSTP */
  167.  
  168. /* Define getpagesize () if the system does not.  */
  169. #define getpagesize() 2048
  170.  
  171. #ifndef BSD42
  172. #ifndef USG
  173. #include <sys/vlimit.h>        /* warn the user when near the end */
  174. #endif /* not USG */
  175. #else /* if BSD42 */
  176. #include <sys/time.h>
  177. /* #include <sys/resource.h> */
  178. #endif /* BSD42 */
  179.  
  180. extern char *start_of_data ();
  181.  
  182. #ifdef BSD
  183. #ifndef DATA_SEG_BITS
  184. #define start_of_data() &etext
  185. #endif
  186. #endif
  187.  
  188. #ifndef emacs
  189. #define start_of_data() &etext
  190. #endif
  191.  
  192. #define ISALLOC ((char) 0xf7)    /* magic byte that implies allocation */
  193. #define ISFREE ((char) 0x54)    /* magic byte that implies free block */
  194.                 /* this is for error checking only */
  195. #define ISMEMALIGN ((char) 0xd6)  /* Stored before the value returned by
  196.                      memalign, with the rest of the word
  197.                      being the distance to the true
  198.                      beginning of the block.  */
  199.  
  200. extern char etext;
  201.  
  202. /* These two are for user programs to look at, when they are interested.  */
  203.  
  204. unsigned int malloc_sbrk_used;       /* amount of data space used now */
  205. unsigned int malloc_sbrk_unused;     /* amount more we can have */
  206.  
  207. /* start of data space; can be changed by calling init_malloc */
  208. static char *data_space_start;
  209.  
  210. #define PAGEWIDTH 11
  211. char *alloc_page();
  212. #define sbrk our_sbrk
  213. char *
  214. our_sbrk(x)
  215. int x;
  216. {return alloc_page((x >> PAGEWIDTH));}
  217.  
  218.  
  219.  
  220.  
  221. #ifdef MSTATS
  222. static int nmalloc[30];
  223. static int nmal, nfre;
  224. #endif /* MSTATS */
  225.  
  226. /* If range checking is not turned on, all we have is a flag indicating
  227.    whether memory is allocated, an index in nextf[], and a size field; to
  228.    realloc() memory we copy either size bytes or 1<<(index+3) bytes depending
  229.    on whether the former can hold the exact size (given the value of
  230.    'index').  If range checking is on, we always need to know how much space
  231.    is allocated, so the 'size' field is never used. */
  232.  
  233. struct mhead {
  234.     char     mh_alloc;    /* ISALLOC or ISFREE */
  235.     char     mh_index;    /* index in nextf[] */
  236. /* Remainder are valid only when block is allocated */
  237.     unsigned short mh_size;    /* size, if < 0x10000 */
  238. #ifdef rcheck
  239.     unsigned mh_nbytes;    /* number of bytes allocated */
  240.     int      mh_magic4;    /* should be == MAGIC4 */
  241. #endif /* rcheck */
  242. };
  243.  
  244. /* Access free-list pointer of a block.
  245.   It is stored at block + 4.
  246.   This is not a field in the mhead structure
  247.   because we want sizeof (struct mhead)
  248.   to describe the overhead for when the block is in use,
  249.   and we do not want the free-list pointer to count in that.  */
  250.  
  251. #define CHAIN(a) \
  252.   (*(struct mhead **) (sizeof (char *) + (char *) (a)))
  253.  
  254. #ifdef rcheck
  255.  
  256. /* To implement range checking, we write magic values in at the beginning and
  257.    end of each allocated block, and make sure they are undisturbed whenever a
  258.    free or a realloc occurs. */
  259. /* Written in each of the 4 bytes following the block's real space */
  260. #define MAGIC1 0x55
  261. /* Written in the 4 bytes before the block's real space */
  262. #define MAGIC4 0x55555555
  263. #define ASSERT(p) if (!(p)) botch("p"); else
  264. #define EXTRA  4        /* 4 bytes extra for MAGIC1s */
  265. #else
  266. #define ASSERT(p)
  267. #define EXTRA  0
  268. #endif /* rcheck */
  269.  
  270.  
  271. /* nextf[i] is free list of blocks of size 2**(i + 3)  */
  272.  
  273. static struct mhead *nextf[30];
  274.  
  275. /* busy[i] is nonzero while allocation of block size i is in progress.  */
  276.  
  277. static char busy[30];
  278.  
  279. /* Number of bytes of writable memory we can expect to be able to get */
  280. static unsigned int lim_data;
  281.  
  282. /* Level number of warnings already issued.
  283.   0 -- no warnings issued.
  284.   1 -- 75% warning already issued.
  285.   2 -- 85% warning already issued.
  286. */
  287. static int warnlevel;
  288.  
  289. /* Function to call to issue a warning;
  290.    0 means don't issue them.  */
  291. static void (*warnfunction) ();
  292.  
  293. /* nonzero once initial bunch of free blocks made */
  294. static int gotpool;
  295.  
  296. char *_malloc_base;
  297.  
  298. static void getpool ();
  299.  
  300. /* Cause reinitialization based on job parameters;
  301.   also declare where the end of pure storage is. */
  302. void
  303. malloc_init (start, warnfun)
  304.      char *start;
  305.      void (*warnfun) ();
  306. {
  307.   if (start)
  308.     data_space_start = start;
  309.   lim_data = 0;
  310.   warnlevel = 0;
  311.   warnfunction = warnfun;
  312. }
  313.  
  314. /* Return the maximum size to which MEM can be realloc'd
  315.    without actually requiring copying.  */
  316.  
  317. int
  318. malloc_usable_size (mem)
  319.      char *mem;
  320. {
  321.   int blocksize = 8 << (((struct mhead *) mem) - 1) -> mh_index;
  322.  
  323.   return blocksize - sizeof (struct mhead) - EXTRA;
  324. }
  325.  
  326. static void
  327. morecore (nu)            /* ask system for more memory */
  328.      register int nu;        /* size index to get more of  */
  329. {
  330.   char *sbrk ();
  331.   register char *cp;
  332.   register int nblks;
  333.   register unsigned int siz;
  334.   int oldmask;
  335.  
  336. #ifdef BSD
  337. #ifndef BSD4_1
  338.   oldmask = sigsetmask (-1);
  339. #endif
  340. #endif
  341.  
  342.   if (!data_space_start)
  343.     {
  344.       data_space_start = start_of_data ();
  345.     }
  346.  
  347.   if (lim_data == 0)
  348.     get_lim_data ();
  349.  
  350.  /* On initial startup, get two blocks of each size up to 1k bytes */
  351.   if (!gotpool)
  352.     { getpool (); getpool (); gotpool = 1; }
  353.  
  354.   /* Find current end of memory and issue warning if getting near max */
  355.  
  356.  /* Take at least 2k, and figure out how many blocks of the desired size
  357.     we're about to get */
  358.   nblks = 1;
  359.   if ((siz = nu) < 8)
  360.     nblks = 1 << ((siz = 8) - nu);
  361.  
  362.   if ((cp = sbrk (1 << (siz + 3)))==0)
  363.     return;            /* no more room! */
  364.  
  365.  /* save new header and link the nblks blocks together */
  366.   nextf[nu] = (struct mhead *) cp;
  367.   siz = 1 << (nu + 3);
  368.   while (1)
  369.     {
  370.       ((struct mhead *) cp) -> mh_alloc = ISFREE;
  371.       ((struct mhead *) cp) -> mh_index = nu;
  372.       if (--nblks <= 0) break;
  373.       CHAIN ((struct mhead *) cp) = (struct mhead *) (cp + siz);
  374.       cp += siz;
  375.     }
  376.   CHAIN ((struct mhead *) cp) = 0;
  377.  
  378. #ifdef BSD
  379. #ifndef BSD4_1
  380.   sigsetmask (oldmask);
  381. #endif
  382. #endif
  383. }
  384.  
  385. static void
  386. getpool ()
  387. {
  388.   register int nu;
  389.   char * sbrk ();
  390.   register char *cp = sbrk (0);
  391.  
  392.   if ((int) cp & 0x3ff)    /* land on 1K boundaries */
  393.     sbrk (1024 - ((int) cp & 0x3ff));
  394.  
  395.   /* Record address of start of space allocated by malloc.  */
  396.   if (_malloc_base == 0)
  397.     _malloc_base = cp;
  398.  
  399.   /* Get 2k of storage */
  400.  
  401.   cp = sbrk (04000);
  402.   if (cp == (char *) -1)
  403.     return;
  404.  
  405.   /* Divide it into an initial 8-word block
  406.      plus one block of size 2**nu for nu = 3 ... 10.  */
  407.  
  408.   CHAIN (cp) = nextf[0];
  409.   nextf[0] = (struct mhead *) cp;
  410.   ((struct mhead *) cp) -> mh_alloc = ISFREE;
  411.   ((struct mhead *) cp) -> mh_index = 0;
  412.   cp += 8;
  413.  
  414.   for (nu = 0; nu < 7; nu++)
  415.     {
  416.       CHAIN (cp) = nextf[nu];
  417.       nextf[nu] = (struct mhead *) cp;
  418.       ((struct mhead *) cp) -> mh_alloc = ISFREE;
  419.       ((struct mhead *) cp) -> mh_index = nu;
  420.       cp += 8 << nu;
  421.     }
  422. }
  423.  
  424. char *
  425. malloc (n)        /* get a block */
  426.      unsigned n;
  427. {
  428.   register struct mhead *p;
  429.   register unsigned int nbytes;
  430.   register int nunits = 0;
  431.  
  432.   /* Figure out how many bytes are required, rounding up to the nearest
  433.      multiple of 4, then figure out which nextf[] area to use */
  434.   nbytes = (n + sizeof *p + EXTRA + 3) & ~3;
  435.   {
  436.     register unsigned int   shiftr = (nbytes - 1) >> 2;
  437.  
  438.     while (shiftr >>= 1)
  439.       nunits++;
  440.   }
  441.  
  442.   /* In case this is reentrant use of malloc from signal handler,
  443.      pick a block size that no other malloc level is currently
  444.      trying to allocate.  That's the easiest harmless way not to
  445.      interfere with the other level of execution.  */
  446.   while (busy[nunits]) nunits++;
  447.   busy[nunits] = 1;
  448.  
  449.   /* If there are no blocks of the appropriate size, go get some */
  450.   /* COULD SPLIT UP A LARGER BLOCK HERE ... ACT */
  451.   if (nextf[nunits] == 0)
  452.     morecore (nunits);
  453.  
  454.   /* Get one block off the list, and set the new list head */
  455.   if ((p = nextf[nunits]) == 0)
  456.     {
  457.       busy[nunits] = 0;
  458.       return 0;
  459.     }
  460.   nextf[nunits] = CHAIN (p);
  461.   busy[nunits] = 0;
  462.  
  463.   /* Check for free block clobbered */
  464.   /* If not for this check, we would gobble a clobbered free chain ptr */
  465.   /* and bomb out on the NEXT allocate of this size block */
  466.   if (p -> mh_alloc != ISFREE || p -> mh_index != nunits)
  467. #ifdef rcheck
  468.     botch ("block on free list clobbered");
  469. #else /* not rcheck */
  470.     abort ();
  471. #endif /* not rcheck */
  472.  
  473.   /* Fill in the info, and if range checking, set up the magic numbers */
  474.   p -> mh_alloc = ISALLOC;
  475. #ifdef rcheck
  476.   p -> mh_nbytes = n;
  477.   p -> mh_magic4 = MAGIC4;
  478.   {
  479.     register char  *m = (char *) (p + 1) + n;
  480.  
  481.     *m++ = MAGIC1, *m++ = MAGIC1, *m++ = MAGIC1, *m = MAGIC1;
  482.   }
  483. #else /* not rcheck */
  484.   p -> mh_size = n;
  485. #endif /* not rcheck */
  486. #ifdef MSTATS
  487.   nmalloc[nunits]++;
  488.   nmal++;
  489. #endif /* MSTATS */
  490.   return (char *) (p + 1);
  491. }
  492.  
  493. free (mem)
  494.      char *mem;
  495. {
  496.   register struct mhead *p;
  497.   {
  498.     register char *ap = mem;
  499.  
  500.     if (ap == 0)
  501.       return;
  502.  
  503.     p = (struct mhead *) ap - 1;
  504.     if (p -> mh_alloc == ISMEMALIGN)
  505.       {
  506.     ap -= p->mh_size;
  507.     p = (struct mhead *) ap - 1;
  508.       }
  509.  
  510.     if (p -> mh_alloc != ISALLOC)
  511.       abort ();
  512.  
  513. #ifdef rcheck
  514.     ASSERT (p -> mh_magic4 == MAGIC4);
  515.     ap += p -> mh_nbytes;
  516.     ASSERT (*ap++ == MAGIC1); ASSERT (*ap++ == MAGIC1);
  517.     ASSERT (*ap++ == MAGIC1); ASSERT (*ap   == MAGIC1);
  518. #endif /* rcheck */
  519.   }
  520.   {
  521.     register int nunits = p -> mh_index;
  522.  
  523.     ASSERT (nunits <= 29);
  524.     p -> mh_alloc = ISFREE;
  525.  
  526.     /* Protect against signal handlers calling malloc.  */
  527.     busy[nunits] = 1;
  528.     /* Put this block on the free list.  */
  529.     CHAIN (p) = nextf[nunits];
  530.     nextf[nunits] = p;
  531.     busy[nunits] = 0;
  532.  
  533. #ifdef MSTATS
  534.     nmalloc[nunits]--;
  535.     nfre++;
  536. #endif /* MSTATS */
  537.   }
  538. }
  539.  
  540. char *
  541. realloc (mem, n)
  542.      char *mem;
  543.      register unsigned n;
  544. {
  545.   register struct mhead *p;
  546.   register unsigned int tocopy;
  547.   register unsigned int nbytes;
  548.   register int nunits;
  549.  
  550.   if ((p = (struct mhead *) mem) == 0)
  551.     return malloc (n);
  552.   p--;
  553.   nunits = p -> mh_index;
  554.   ASSERT (p -> mh_alloc == ISALLOC);
  555. #ifdef rcheck
  556.   ASSERT (p -> mh_magic4 == MAGIC4);
  557.   {
  558.     register char *m = mem + (tocopy = p -> mh_nbytes);
  559.     ASSERT (*m++ == MAGIC1); ASSERT (*m++ == MAGIC1);
  560.     ASSERT (*m++ == MAGIC1); ASSERT (*m   == MAGIC1);
  561.   }
  562. #else /* not rcheck */
  563.   if (p -> mh_index >= 13)
  564.     tocopy = (1 << (p -> mh_index + 3)) - sizeof *p;
  565.   else
  566.     tocopy = p -> mh_size;
  567. #endif /* not rcheck */
  568.  
  569.   /* See if desired size rounds to same power of 2 as actual size. */
  570.   nbytes = (n + sizeof *p + EXTRA + 7) & ~7;
  571.  
  572.   /* If ok, use the same block, just marking its size as changed.  */
  573.   if (nbytes > (4 << nunits) && nbytes <= (8 << nunits))
  574.     {
  575. #ifdef rcheck
  576.       register char *m = mem + tocopy;
  577.       *m++ = 0;  *m++ = 0;  *m++ = 0;  *m++ = 0;
  578.       p-> mh_nbytes = n;
  579.       m = mem + n;
  580.       *m++ = MAGIC1;  *m++ = MAGIC1;  *m++ = MAGIC1;  *m++ = MAGIC1;
  581. #else /* not rcheck */
  582.       p -> mh_size = n;
  583. #endif /* not rcheck */
  584.       return mem;
  585.     }
  586.  
  587.   if (n < tocopy)
  588.     tocopy = n;
  589.   {
  590.     register char *new;
  591.  
  592.     if ((new = malloc (n)) == 0)
  593.       return 0;
  594.     bcopy (mem, new, tocopy);
  595.     free (mem);
  596.     return new;
  597.   }
  598. }
  599.  
  600. #ifndef VMS
  601.  
  602. char *
  603. memalign (alignment, size)
  604.      unsigned alignment, size;
  605. {
  606.   register char *ptr = malloc (size + alignment);
  607.   register char *aligned;
  608.   register struct mhead *p;
  609.  
  610.   if (ptr == 0)
  611.     return 0;
  612.   /* If entire block has the desired alignment, just accept it.  */
  613.   if (((int) ptr & (alignment - 1)) == 0)
  614.     return ptr;
  615.   /* Otherwise, get address of byte in the block that has that alignment.  */
  616.   aligned = (char *) (((int) ptr + alignment - 1) & -alignment);
  617.  
  618.   /* Store a suitable indication of how to free the block,
  619.      so that free can find the true beginning of it.  */
  620.   p = (struct mhead *) aligned - 1;
  621.   p -> mh_size = aligned - ptr;
  622.   p -> mh_alloc = ISMEMALIGN;
  623.   return aligned;
  624. }
  625.  
  626. #ifndef HPUX
  627. /* This runs into trouble with getpagesize on HPUX.
  628.    Patching out seems cleaner than the ugly fix needed.  */
  629. char *
  630. valloc (size)
  631. {
  632.   return memalign (getpagesize (), size);
  633. }
  634. #endif /* not HPUX */
  635. #endif /* not VMS */
  636.  
  637. #ifdef MSTATS
  638. /* Return statistics describing allocation of blocks of size 2**n. */
  639.  
  640. struct mstats_value
  641.   {
  642.     int blocksize;
  643.     int nfree;
  644.     int nused;
  645.   };
  646.  
  647. struct mstats_value
  648. malloc_stats (size)
  649.      int size;
  650. {
  651.   struct mstats_value v;
  652.   register int i;
  653.   register struct mhead *p;
  654.  
  655.   v.nfree = 0;
  656.  
  657.   if (size < 0 || size >= 30)
  658.     {
  659.       v.blocksize = 0;
  660.       v.nused = 0;
  661.       return v;
  662.     }
  663.  
  664.   v.blocksize = 1 << (size + 3);
  665.   v.nused = nmalloc[size];
  666.  
  667.   for (p = nextf[size]; p; p = CHAIN (p))
  668.     v.nfree++;
  669.  
  670.   return v;
  671. }
  672. #endif /* MSTATS */
  673.  
  674. /*
  675.  *    This function returns the total number of bytes that the process
  676.  *    will be allowed to allocate via the sbrk(2) system call.  On
  677.  *    BSD systems this is the total space allocatable to stack and
  678.  *    data.  On USG systems this is the data space only.
  679.  */
  680.  
  681. #ifdef USG
  682.  
  683. get_lim_data ()
  684. {
  685.   extern long ulimit ();
  686.     
  687.   lim_data = ulimit (3, 0);
  688.   lim_data -= (long) data_space_start;
  689. }
  690.  
  691. #else /* not USG */
  692. #ifndef BSD42
  693.  
  694. get_lim_data ()
  695. {
  696.   lim_data = vlimit (LIM_DATA, -1);
  697. }
  698.  
  699. #else /* BSD42 */
  700.  
  701. get_lim_data ()
  702. {
  703.   struct rlimit XXrlimit;
  704.  
  705.   getrlimit (RLIMIT_DATA, &XXrlimit);
  706. #ifdef RLIM_INFINITY
  707.   lim_data = XXrlimit.rlim_cur & RLIM_INFINITY; /* soft limit */
  708. #else
  709.   lim_data = XXrlimit.rlim_cur;    /* soft limit */
  710. #endif
  711. }
  712.  
  713. #endif /* BSD42 */
  714. #endif /* not USG */
  715.  
  716. #ifdef VMS
  717. /* There is a problem when dumping and restoring things on VMS. Calls
  718.  * to SBRK don't necessarily result in contiguous allocation. Dumping
  719.  * doesn't work when it isn't. Therefore, we make the initial
  720.  * allocation contiguous by allocating a big chunk, and do SBRKs from
  721.  * there. Once Emacs has dumped there is no reason to continue
  722.  * contiguous allocation, malloc doesn't depend on it.
  723.  *
  724.  * There is a further problem of using brk and sbrk while using VMS C
  725.  * run time library routines malloc, calloc, etc. The documentation
  726.  * says that this is a no-no, although I'm not sure why this would be
  727.  * a problem. In any case, we remove the necessity to call brk and
  728.  * sbrk, by calling calloc (to assure zero filled data) rather than
  729.  * sbrk.
  730.  *
  731.  * VMS_ALLOCATION_SIZE is the size of the allocation array. This
  732.  * should be larger than the malloc size before dumping. Making this
  733.  * too large will result in the startup procedure slowing down since
  734.  * it will require more space and time to map it in.
  735.  *
  736.  * The value for VMS_ALLOCATION_SIZE in the following define was determined
  737.  * by running emacs linked (and a large allocation) with the debugger and
  738.  * looking to see how much storage was used. The allocation was 201 pages,
  739.  * so I rounded it up to a power of two.
  740.  */
  741. #ifndef VMS_ALLOCATION_SIZE
  742. #define VMS_ALLOCATION_SIZE    (512*256)
  743. #endif
  744.  
  745. /* Use VMS RTL definitions */
  746. #undef sbrk
  747. #undef brk
  748. #undef malloc
  749. int vms_out_initial = 0;
  750. char vms_initial_buffer[VMS_ALLOCATION_SIZE];
  751. static char *vms_current_brk = &vms_initial_buffer;
  752. static char *vms_end_brk = &vms_initial_buffer[VMS_ALLOCATION_SIZE-1];
  753.  
  754. #include <stdio.h>
  755.  
  756. char *
  757. sys_sbrk (incr)
  758.      int incr;
  759. {
  760.   char *sbrk(), *temp, *ptr;
  761.  
  762.   if (vms_out_initial)
  763.     {
  764.       /* out of initial allocation... */
  765.       if (!(temp = malloc (incr)))
  766.     temp = (char *) -1;
  767.     }
  768.   else
  769.     {
  770.       /* otherwise, go out of our area */
  771.       ptr = vms_current_brk + incr; /* new current_brk */
  772.       if (ptr <= vms_end_brk)
  773.     {
  774.       temp = vms_current_brk;
  775.       vms_current_brk = ptr;
  776.     }
  777.       else
  778.     {
  779.       vms_out_initial = 1;    /* mark as out of initial allocation */
  780.       if (!(temp = malloc (incr)))
  781.         temp = (char *) -1;
  782.     }
  783.     }
  784.   return temp;
  785. }
  786. #endif /* VMS */
  787.